<?php
// Iniciando sessão
session_start();

// Incluindo classe e configurações
require_once('../twitteroauth/twitteroauth.php');
require_once('../config.php');

// Se os tokens de acesso não estiverem setados
//if (!isset($_SESSION['access_token']))
    // Redirecionando para paǵina responsável por limpar a sessão e redirecionar para página de acesso
    //header('Location: ../clearsessions.php');

// Armazenando tokens de acesso
//$access_token = $_SESSION['access_token'];

// Instanciando objeto para acessar a API
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, USER_TOKEN, USER_SECRET);//$access_token['oauth_token'], $access_token['oauth_token_secret']);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

    <head>
        <title>Acessando API do twitter via OAuth com PHP</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        
        <style type="text/css">
        body {
            font-family: Arial;
            font-size: 12px; 
        }
        
        img {
            border: 0;
            width: 48px;
            height: 48px;
        }
        
        h1 {
            font-size: 20px;
            color: maroon;
            border-bottom: 1px dashed #CCC;
        }
        
        /* Usuário */
        #usuario img {
            float: left;
            margin-right: 5px;
        }
        
        #usuario h3 {
            margin-bottom: 0;
        }
        
        /* Atualizações */
        div.tweet img {
            float: left;
            margin-right: 5px;
        }
        
        div.tweet {
            clear: both;
            margin-bottom: 10px;
        }
        
        div.tweet .usuario big {
            font-weight: bold;
        }
        </style>
    </head>
    
    <body>
      
        <h1>Aplicação twitter</h1>
        
        <p>
          <a href="../clearsessions.php">Sair</a>
        </p>
        
        <div id="usuario">
            
            <?php
            // @link http://dev.twitter.com/doc/get/account/verify_credentials
            $usuario = $connection->get('account/verify_credentials'); ?>
            
            <img src='<?php echo $usuario->profile_image_url ?>' />
            
            <h3><?php echo $usuario->name ?></h3>
            
            <big><strong><?php echo '@' . $usuario->screen_name ?></strong></big>
            <span class="location"><?php echo $usuario->location ?></span>
            
            <p>
                <em><?php echo $usuario->description ?></em> <br />
                <a href='<?php echo $usuario->url ?>'><?php echo $usuario->url ?></a>
            </p>

        </div>
        
        <h1>Atualizações de amigos</h1>
        
        <div id="timeline">
        
            <?php
            // @link http://dev.twitter.com/doc/get/statuses/home_timeline
            $timeline = $connection->get('statuses/home_timeline', array('count' => 5)); ?>
            
            <?php foreach ($timeline as $tweet): ?>
                
                <div class="tweet">
                
                    <img src='<?php echo $tweet->user->profile_image_url ?>' />
                    
                    <span class="usuario">
                        <big><a href="http://twitter.com/<?php echo $tweet->user->screen_name ?>/"><?php echo $tweet->user->screen_name ?></a></big> 
                        <?php echo $tweet->user->name ?>
                    </span>
                    
                    <br />
                    
                    <span class="texto">
                        <?php echo $tweet->text ?>
                    </span>   
                    
                    <br />
                    
                    <span class="data">
                        <?php echo date('d/m/Y H:i:s', strtotime($tweet->created_at)) ?>
                    </span>    
                </div>
                
            <?php endforeach ?>

        </div>
        
        <h1>Seguindo</h1>
        
        <div id="following">
            
            <?php
            // @link http://dev.twitter.com/doc/get/statuses/friends
            $friends = $connection->get('statuses/friends'); ?>
            
            <p><em>Exibindo 20 amigos de <?php echo $usuario->friends_count ?>.</em> </p>

            <?php foreach ($friends as $friend): ?>
                
                <a href="http://twitter.com/<?php echo $friend->screen_name ?>">
                    <img src='<?php echo $friend->profile_image_url ?>' title='<?php echo $friend->screen_name ?>' />
                </a>
            
            <?php endforeach ?>
            
        </div>
        
        <h1>Seguidores</h1>
        
        <div id="followers">
            
            <?php
            // @link http://dev.twitter.com/doc/get/statuses/followers
            $followers = $connection->get('statuses/followers', array('count' => 20)); ?>
            
            <p><em>Exibindo 20 seguidores de <?php echo $usuario->followers_count ?>.</em></p>
            
            <?php foreach ($followers as $follower): ?>
            
                <a href="http://twitter.com/<?php echo $follower->screen_name ?>">
                    <img src='<?php echo $follower->profile_image_url ?>' />
                </a>
            
            <?php endforeach ?>
            
        </div>
        
        <h1>Trends: Brasil</h1>
        
        <div id="trends">
            
            <?php
            // @link http://dev.twitter.com/doc/get/trends/:woeid
            $trends = $connection->get('trends/23424768'); ?>
            
            <ul>
                <?php foreach ($trends[0]->trends as $trend): ?>
                    <li><a href="http://twitter.com/search/<?php echo $trend->query ?>"><?php echo $trend->name ?></a></li>
                <?php endforeach ?>
            </ul>
            
        </div>
        
        <h1>Atualizações de <em>@marlonlp</em></h1>
        
        <div id="timeline-faael">
            
            <?php
            // @link http://dev.twitter.com/doc/get/statuses/user_timeline
            $timeline = $connection->get('statuses/user_timeline', array('screen_name' => 'marlonlp')); ?>
            
            <?php foreach ($timeline as $tweet): ?>
                
                <div class="tweet">
                
                    <img src='<?php echo $tweet->user->profile_image_url ?>' />
                    
                    <span class="usuario">
                        <big><a href="http://twitter.com/<?php echo $tweet->user->screen_name ?>/"><?php echo $tweet->user->screen_name ?></a></big> 
                        <?php echo $tweet->user->name ?>
                    </span>
                    
                    <br />
                    
                    <span class="texto">
                        <?php echo $tweet->text ?>
                    </span>   
                    
                    <br />
                    
                    <span class="data">
                        <?php echo date('d/m/Y H:i:s', strtotime($tweet->created_at)) ?>
                    </span>    
                </div>
                
            <?php endforeach ?>
            
        </div>

  </body>
</html>